package com.yanqu.road.server.manger.activity;

import com.yanqu.road.logic.bussiness.player.UserActivityBussiness;
import com.yanqu.road.entity.activity.TurntableLotteryRecord;
import com.yanqu.road.pb.activity.TurntableProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.ActivityPb;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.UUIDHelper;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class TurntableMgr extends TempMgr {

    private static LinkedList<TurntableLotteryRecord> turntableLotteryRecordList = new LinkedList<>();

    private static final int MAX_TURNTABLE_LOTTERY_RECORD_COUNT = 5;

    @Override
    public boolean reloadData() {
        turntableLotteryRecordList = UserActivityBussiness.getTurntableLotteryRecordList(MAX_TURNTABLE_LOTTERY_RECORD_COUNT);
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        List<TurntableLotteryRecord> tempList = new ArrayList<>(turntableLotteryRecordList);
        if(null != tempList){
            for(TurntableLotteryRecord turntableLotteryRecord : tempList){
                if(turntableLotteryRecord.isInsertOption()){
                    UserActivityBussiness.addTurntableLotteryRecord(turntableLotteryRecord);
                }else if(turntableLotteryRecord.isUpdateOption()){
                    UserActivityBussiness.updateTurntableLotteryRecord(turntableLotteryRecord);
                }
            }
        }
        return true;
    }

    public static void addTurntableLotteryRecord(GamePlayer player, Property property){
        if(null == player || null == property){
            return;
        }
        int goodsId = 0;
        int count = 0;
        for(Map.Entry<Integer, BigInteger> dataEntry : property.getGoods().entrySet()){
            goodsId = dataEntry.getKey();
            count = dataEntry.getValue().intValue();
            break;
        }
        if(0 == goodsId || 0 == count){
            return;
        }
        TurntableLotteryRecord turntableLotteryRecord = new TurntableLotteryRecord();
        turntableLotteryRecord.setRecordUid(UUIDHelper.randomUUID(String.valueOf(GameServer.getInstance().getServerId())));
        turntableLotteryRecord.setUserId(player.getUserId());
        turntableLotteryRecord.setNickName(player.getUserInfo().getNickName());
        turntableLotteryRecord.setRecordTime(System.currentTimeMillis() / 1000);
        turntableLotteryRecord.setGoodsId(goodsId);
        turntableLotteryRecord.setCount(count);
        turntableLotteryRecord.setInsertOption();
        addTurntableLotteryRecord(turntableLotteryRecord);
    }

    public static void addTurntableLotteryRecord(TurntableLotteryRecord turntableLotteryRecord){
        if(null == turntableLotteryRecord){
            return;
        }
        synchronized (turntableLotteryRecordList){
            if(turntableLotteryRecordList.size() >= MAX_TURNTABLE_LOTTERY_RECORD_COUNT){
                turntableLotteryRecordList.removeLast();
            }
            turntableLotteryRecordList.addFirst(turntableLotteryRecord);
        }
        noticeTurntableLotteryRecord(turntableLotteryRecord);
    }

    private static void noticeTurntableLotteryRecord(TurntableLotteryRecord turntableLotteryRecord){
        if(null == turntableLotteryRecord){
            return;
        }
        List<GamePlayer> playerList = GamePlayerMgr.getCopyPlayerList();
        if(null != playerList){
            List<TurntableLotteryRecord> syncList = new ArrayList<>();
            syncList.add(turntableLotteryRecord);
            TurntableProto.TurntableLotteryRecordListMsg.Builder listMsg = ActivityPb.parseTurntableLotteryRecordListMsg(syncList);
            for(GamePlayer player : playerList){
                player.sendPacket(Protocol.U_TURNTABLE_LOTTERY_RECORD_SYNC, listMsg);
            }
        }
    }

    public static List<TurntableLotteryRecord> getRecordList(){
        List<TurntableLotteryRecord> recordList;
        synchronized (turntableLotteryRecordList){
            recordList = new ArrayList<>(turntableLotteryRecordList);
        }
        return recordList;
    }
}
